
import { message } from 'antd';
import { queryRole, addRole, updateRole, deleteRole, changeRoleStatus, getOptionselect } from '../services/API';
export default {

  namespace: 'role',

  state: {
    listData: [],//table数据
    fieldData: [],//删选dom,
    total: 0,//总页数
    optionselect: [],// 角色选择框列表
    pagination: {
      total: 0,
      current: 1
    },
    querySearch: {
      pageSize: 20,
      pageNum: 1
    }
  },

  subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line
      history.listen(({ pathname }) => {
        if (pathname === '/Role') {

          dispatch({
            type: "role/fetch"
          });
          dispatch({
            type: `role/getOptionselect`
          })
        }
      });
    },
  },

  effects: {

    *fetch({ payload }, { call, put, select }) {  // eslint-disable-line
      const querySearch = yield select(state => state.role.querySearch);
      const res = yield call(queryRole, { ...querySearch, ...payload });
      if (res.code === 200) {
        yield put({
          type: "save",
          payload: {
            listData: res.rows,
            pagination: {
              total: res.total,
              current: querySearch.pageNum
            }
          }
        })
      } else {
        message.error(res.msg || '失败')
      }
    },
    *getOptionselect({ payload, callback }, { call, put }) {
      const res = yield call(getOptionselect, payload);

      if (res.code === 200) {
        yield put({
          type: "save",
          payload: {
            optionselect: res.data
          }
        })
      } else {
        message.error(res.msg || '失败')
      }
      callback && callback();
    },
    *add({ payload }, { call, put }) {  // eslint-disable-line
      const res = yield call(addRole, payload);
      if (res.code === 200) {
        message.success(res.msg || 'Operation succeeded')
        yield put({
          type: "fetch",
        })
      } else {
        message.error(res.msg || '失败')
      }
    },
    *update({ payload }, { call, put }) {  // eslint-disable-line
      const res = yield call(updateRole, payload);
      if (res.code === 200) {
        message.success(res.msg || 'Operation succeeded')
        yield put({
          type: "fetch",
        })
      } else {
        message.error(res.msg || '失败')
      }

    },
    *delete({ payload }, { call, put }) {  // eslint-disable-line
      const res = yield call(deleteRole, payload);
      if (res.code === 200) {
        message.success(res.msg || 'Operation succeeded')
        yield put({
          type: "fetch",
        })
      } else {
        message.error(res.msg || '失败')
      }
      // message.success(res.msg)  
    },
    *changeStatus({ payload }, { call, put }) {  // eslint-disable-line
      const res = yield call(changeRoleStatus, payload);
      if (res.code === 200) {
        message.success(res.msg || 'Operation succeeded')
        yield put({
          type: "fetch",
        })
      } else {
        message.error(res.msg || '失败')
      }

    },
  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },
  },

};
